package com.backstage.config;

import cn.hutool.core.lang.Snowflake;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

/**
 * 数据源的配置类，用来代替配置文件
 */
@Configuration
@PropertySource("classpath:config/database.properties")
public class DatasoureceConfiguration {
    @Bean("dataSource")
    @ConditionalOnMissingBean(name = "dataSource")
    public DataSource druidDataSource(@Value("${driver}") String driver,
                                      @Value("${url}") String url,
                                      @Value("${user}") String username,
                                      @Value("${password}") String password) {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(url);
        druidDataSource.setDriverClassName(driver);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        druidDataSource.setMaxActive(10);
        druidDataSource.setMinIdle(2);
        druidDataSource.setQueryTimeout(100);
        return druidDataSource;
    }

}
